package cn.mogugo.joke.manager;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import cn.mogugo.comm.BaseManager;
import cn.mogugo.comm.PageSupport;
import cn.mogugo.joke.model.JokeObj;
import cn.mogugo.util.tools.NumberTools;

public class JokeObjManager extends BaseManager{

	public List getAll(){
		String hql = "from JokeObj jo order by jo.id asc";
		return this.baseDao.getByHQL(hql, null);
	}
	
	
	public List getAll(Integer fromId , Integer endId){
		String hql = "from JokeObj jo where id >=:fromId and id <=:endId order by jo.id asc";
		Map map = new HashMap();
		map.put("fromId", fromId);
		map.put("endId", endId);
		return this.baseDao.getByHQL(hql, map);
	}
	
	
	
	public List getAllNoKeyword(){
		String hql = "from JokeObj jo where jo.key_words is null order by jo.id asc";
		return this.baseDao.getByHQL(hql, null);
	}
	
	public Integer getNumberByCategoryId(Integer categoryId){
		String hql = "select count(jo.jokeCategory.id) as c from JokeObj jo where jo.jokeCategory.id =:categoryId";
		Map map = new HashMap();
		map.put("categoryId", categoryId);
		Object obj = this.baseDao.getUniqueBy(hql, map);
		return ((Long)obj).intValue();
	}
	
	public JokeObj getById(Integer id){
		return (JokeObj)this.baseDao.get(JokeObj.class, id);
	}
	
	public void saveObj(JokeObj jo){
		baseDao.saveOrUpdate(jo);
	}
	
	
	public void updateListToAnotherCategory(Integer fromId , Integer toId){
		String hql = "update JokeObj jo set jo.jokeCategory.id =:toId where jo.jokeCategory.id =:fromId ";
		Map mapValue = new HashMap();
		mapValue.put("toId", toId);
		mapValue.put("fromId", fromId);
		this.baseDao.excuteByHql(hql, mapValue);
	}
	
	
	public List getListByCategoryId(Integer categoryId){
		String hql = "from JokeObj jo where jo.jokeCategory.id =:categoryId ";
		Map mapValue = new HashMap();
		mapValue.put("categoryId", categoryId);
		return this.baseDao.getByHQL(hql, mapValue);
	}
	
	
	public PageSupport getPage(Map map , Integer pageNo , Integer pageSize , String orderBy){
		//System.out.println(orderBy);
		
		String hql = "from JokeObj jo where 1=1 ";
		Map mapValue = new HashMap();
		
		if(map != null){
			Object objJokeTitle = map.get("joke_title");
			if(objJokeTitle != null){
				String joke_title = (String)objJokeTitle;
				hql += " and jo.joke_title like:joke_title ";
				mapValue.put("joke_title", "%"+joke_title+"%");
			}
			Object objCategoryId = map.get("categoryId");
			if(objCategoryId != null){
				Integer categoryId = (Integer)objCategoryId;
				if(categoryId.equals("0")){
					hql += " and jo.jokeCategory is null ";
				}else{
					hql += " and jo.jokeCategory.id =:categoryId ";
					mapValue.put("categoryId", categoryId);
				}
				
			}
			
			
			Object objId = map.get("id");
			if(objId != null){
				Integer id = (Integer)objId;
				hql += " and jo.id =:id ";
				mapValue.put("id", id);
			}
			
			
			Object objAuditing = map.get("auditing");
			if(objAuditing != null){
				Integer auditing = (Integer)objAuditing;
				hql += " and jo.auditing =:auditing ";
				mapValue.put("auditing", auditing);
			}
		}
		if(orderBy.equals("new")){
			hql += "  order by jo.joke_date desc  , id desc";
		}else if(orderBy.equals("hot")){
			hql += "  order by jo.click_number desc , jo.id desc";
		}else{
			hql += "  order by jo.joke_date desc , id desc";
		}
		
		return this.baseDao.findPageByHql(hql, pageSize, pageNo, mapValue);
	}
	
	public PageSupport getSameTitleJo(Integer pageNo , Integer pageSize){
		String sql = "select * from ( select JOKE_TITLE , count(JOKE_TITLE) as SN from JOKE_OBJ where AUDITING != 2 group by JOKE_TITLE ) as CC where CC.SN > 1 order by CC.SN asc ";
		return this.baseDao.findPageBySql(sql, pageSize, pageNo, null);
	}
	
	
	public List getSameTitleJo(){
		String sql = "select * from ( select JOKE_TITLE , count(JOKE_TITLE) as SN from JOKE_OBJ where AUDITING != 2 group by JOKE_TITLE ) as CC where CC.SN > 1 order by CC.SN asc ";
		return this.baseDao.getBySql(sql, null);
	}
	
	public JokeObj getNextAndLastJo(JokeObj jokeObj , String strType , String categoryId){
		String hql = "";
		Map mapValue = new HashMap();
		String strOrderBy = "";
		if(strType.equals("next")){
			hql = "from JokeObj jo where jo.joke_date <:joke_date ";
			mapValue.put("joke_date", jokeObj.getJoke_date());
			strOrderBy = " order by jo.joke_date , id desc";
		}else if(strType.equals("last")){
			hql = "from JokeObj jo where jo.joke_date >:joke_date ";
			mapValue.put("joke_date", jokeObj.getJoke_date());
			strOrderBy = " order by jo.joke_date desc , id asc";
		}
		//
		
		if(StringUtils.isNotBlank(categoryId)){
			if(NumberTools.isNumber(categoryId)){
				hql += " and jo.jokeCategory.id =:categoryId  ";
				mapValue.put("categoryId", Integer.valueOf(categoryId));
			}
		}
		
		hql += strOrderBy;
		
		PageSupport ps = this.baseDao.findPageByHql(hql, 1, 1, mapValue);
		List list = ps.getItems();
		if(list.isEmpty()){
			return null;
		}else{
			return (JokeObj)list.get(0);
		}
		
	}
	
	public List getJokeObjByAuditing(Integer auditing){
		String hql = "from JokeObj jo where jo.auditing =:auditing ";
		Map mapValue = new HashMap();
		mapValue.put("auditing", auditing);
		return this.baseDao.getByHQL(hql, mapValue);
	}
	
	public List getByJokeTitle(String jokeTitle){
		String hql = "from JokeObj jo where jo.joke_title =:jokeTitle ";
		Map mapValue = new HashMap();
		mapValue.put("jokeTitle", jokeTitle);
		return this.baseDao.getByHQL(hql, mapValue);
		
	}
	
	
	
	public void deleteSame(String[] ids){

		for(int i = 0 ; i < ids.length ; i ++ ){
			String id = ids[i].split("_")[1];
			this.baseDao.deleteObjectByIdLog(JokeObj.class, Integer.valueOf(id));

		}
		
	}
	
	
	public List getListRandomByJokeDate(String adate , int listNumber){
		String sql = "select id from JOKE_OBJ where joke_date < '"+adate+"' order by rand() limit "+listNumber;
		return this.baseDao.getBySql(sql, null);
	}
	
	public JokeObj getRandom(){
		String sql = "select id from JOKE_OBJ order by rand() limit 1";
		List list = this.baseDao.getBySql(sql, null);
		Map map = (Map)list.get(0);
		Integer joId = (Integer)map.get("ID");
		
		return (JokeObj)this.baseDao.get(JokeObj.class, joId);
	}
	
	
	public List getBefore20121001(){
		String hql = "from JokeObj jo where jo.joke_date < '2012-10-01' ";
		return this.baseDao.getByHQL(hql, null);
	}
	
}
